package org.friping.mytool.views.picker;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.widget.ListView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.friping.mytool.R;
import org.friping.mytool.helper.Constants;
import org.friping.mytool.helper.UITypeHelper;
import org.friping.mytool.views.custom.adapter.RadioAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EActivity(R.layout.p_day_of_month)
public class PickerDayOfMonthActivity extends PickerActivity {


    @ViewById
    ListView listView;

    @Bean
    RadioAdapter listViewAdapter;

    @Bean
    UITypeHelper uiTypeHelper;

    private List<Map> items = null;

    @Extra
    Boolean isLunar = false;

    @AfterViews
    public void afterView(){
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        // render title
        if(title != null){
            actionBar.setTitle(title);
        }else{
            if(label != null){
                actionBar.setTitle("更改"+label);
            }
        }

        // render value
        initListView();
        setValue((Integer) value);
    }

    @Click(R.id.btnSave)
    public void save(){
        Intent intent=new Intent();

        intent.putExtra("name", name);
        Integer value = getValue();
        intent.putExtra("isLunar", isLunar);
        if(value != null){
            intent.putExtra("value", value);
        }
        setResult(Constants.PICKER_DAY_OF_MONTH_RESULT_CODE,intent);
        finish();
    }

    @ItemClick
    public void listViewItemClicked(int position){
        Map item = (Map) listViewAdapter.getItem(position);
        Integer value = (Integer) item.get("value");
        setValue(value);
    }

    private void initListView() {
        items = new ArrayList();
        Integer length = isLunar ? 30 : 31;
        for(int i = 1; i <= length; i++){
            Map item = new HashMap();
            wrapItemsData(i, item);
            items.add(item);
        }

        listViewAdapter.refresh(items);
        listView.setAdapter(listViewAdapter);
    }

    private void wrapItemsData(int i, Map<String, Object> item){
        item.put("value", i);
        String label = uiTypeHelper.dayOfMonthText(i, isLunar);
        item.put("title", label);
        item.put("checked", false);
    }

    private void setValue(int value){
        for(Map<String, Object> item : items){
            if(item.get("value").equals(value)){
                item.put("checked", true);
            }else{
                item.put("checked", false);
            }
        }
        listViewAdapter.notifyDataSetChanged();
    }

    private Integer getValue(){
        for(Map<String, Object> item : items){
            if(item.get("checked").equals(true)){
                return (Integer) item.get("value");
            }
        }
        return null;
    }
}
